
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>log · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="errors.html" />
    
    
    <link rel="prev" href="start.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    快速入门
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../guides/project.html">
            
                <a href="../guides/project.html">
            
                    
                    项目结构和初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../guides/api.html">
            
                <a href="../guides/api.html">
            
                    
                    API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../guides/doc.html">
            
                <a href="../guides/doc.html">
            
                    
                    生成 API 文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../guides/client.html">
            
                <a href="../guides/client.html">
            
                    
                    生成客户端
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    概念
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../concepts/cp.html">
            
                <a href="../concepts/cp.html">
            
                    
                    Consumer && Producer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../concepts/operator.html">
            
                <a href="../concepts/operator.html">
            
                    
                    Operator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../concepts/filter.html">
            
                <a href="../concepts/filter.html">
            
                    
                    Filter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../concepts/prefab.html">
            
                <a href="../concepts/prefab.html">
            
                    
                    Prefab
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../concepts/modifier.html">
            
                <a href="../concepts/modifier.html">
            
                    
                    Modifier
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../concepts/middleware.html">
            
                <a href="../concepts/middleware.html">
            
                    
                    Middleware
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../concepts/context.html">
            
                <a href="../concepts/context.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../concepts/source.html">
            
                <a href="../concepts/source.html">
            
                    
                    Parameter Generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../concepts/destination.html">
            
                <a href="../concepts/destination.html">
            
                    
                    Destination Handler
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../concepts/error.html">
            
                <a href="../concepts/error.html">
            
                    
                    错误包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../concepts/method.html">
            
                <a href="../concepts/method.html">
            
                    
                    方法包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../concepts/configurer.html">
            
                <a href="../concepts/configurer.html">
            
                    
                    配置器机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../concepts/plugin.html">
            
                <a href="../concepts/plugin.html">
            
                    
                    插件机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="../concepts/clients.html">
            
                <a href="../concepts/clients.html">
            
                    
                    多客户端整合
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    插件
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../plugins/logger.html">
            
                <a href="../plugins/logger.html">
            
                    
                    系统日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../plugins/reqlog.html">
            
                <a href="../plugins/reqlog.html">
            
                    
                    请求日志插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../plugins/metrics.html">
            
                <a href="../plugins/metrics.html">
            
                    
                    监控指标插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../plugins/tracing.html">
            
                <a href="../plugins/tracing.html">
            
                    
                    请求追踪插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../plugins/profiling.html">
            
                <a href="../plugins/profiling.html">
            
                    
                    性能分析插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../plugins/version.html">
            
                <a href="../plugins/version.html">
            
                    
                    版本信息插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../plugins/healthcheck.html">
            
                <a href="../plugins/healthcheck.html">
            
                    
                    健康检查插件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    框架开发者指南
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="start.html">
            
                <a href="start.html">
            
                    
                    准备工作
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.2" data-path="log.html">
            
                <a href="log.html">
            
                    
                    log
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="errors.html">
            
                <a href="errors.html">
            
                    
                    errors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="definition.html">
            
                <a href="definition.html">
            
                    
                    definition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="method.html">
            
                <a href="method.html">
            
                    
                    definition/method
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="converter.html">
            
                <a href="converter.html">
            
                    
                    operators/converter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="validator.html">
            
                <a href="validator.html">
            
                    
                    operators/validator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="router.html">
            
                <a href="router.html">
            
                    
                    service/router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9" data-path="service.html">
            
                <a href="service.html">
            
                    
                    service
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.10" data-path="nirvana.html">
            
                <a href="nirvana.html">
            
                    
                    nirvana
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.11" data-path="config.html">
            
                <a href="config.html">
            
                    
                    config
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.12" data-path="rest.html">
            
                <a href="rest.html">
            
                    
                    rest
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.13" data-path="utils.html">
            
                <a href="utils.html">
            
                    
                    utils
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.14" data-path="command.html">
            
                <a href="command.html">
            
                    
                    cmd/nirvana
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >log</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="log-&#x5305;">log &#x5305;</h1>
<p>log &#x5305;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x7EC4;&#x57FA;&#x7840;&#x7684;&#x65E5;&#x5FD7; API &#x548C;&#x4E00;&#x4E2A;&#x4F7F;&#x7528;&#x4E86; stderr &#x8FDB;&#x884C;&#x8F93;&#x51FA;&#x7684;&#x6807;&#x51C6;&#x5B9E;&#x73B0;&#x3002;</p>
<p>&#x6807;&#x51C6;&#x63A5;&#x53E3;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-comment">// Level is log level of verboser. We strongly recommend you to</span>
<span class="hljs-comment">// follow the rules:</span>
<span class="hljs-comment">// 1. Use LevelDebug for debug log.</span>
<span class="hljs-comment">// 2. Use 1 for important but not frequent log.</span>
<span class="hljs-comment">// 3. Use 2 for important and frequent log.</span>
<span class="hljs-comment">// 4. Use 3 for not important and not frequent log.</span>
<span class="hljs-comment">// 5. Use 4 for not important but frequent log.</span>
<span class="hljs-comment">// 6. Use [5, LevelDebug) only if you want to custom log levels.</span>
<span class="hljs-keyword">type</span> Level <span class="hljs-keyword">int32</span>

<span class="hljs-keyword">const</span> (
    <span class="hljs-comment">// LevelDebug is for debug info.</span>
    LevelDebug Level = math.MaxInt32
)

<span class="hljs-comment">// Severity has four classes to correspond with log situation.</span>
<span class="hljs-keyword">type</span> Severity <span class="hljs-keyword">string</span>

<span class="hljs-keyword">const</span> (
    <span class="hljs-comment">// SeverityInfo is for usual log.</span>
    SeverityInfo Severity = <span class="hljs-string">&quot;INFO&quot;</span>
    <span class="hljs-comment">// SeverityWarning is for warning.</span>
    SeverityWarning Severity = <span class="hljs-string">&quot;WARN&quot;</span>
    <span class="hljs-comment">// SeverityError is for error.</span>
    SeverityError Severity = <span class="hljs-string">&quot;ERROR&quot;</span>
    <span class="hljs-comment">// SeverityFatal is for panic error. The severity means that</span>
    <span class="hljs-comment">// a logger will output the error and exit immediately.</span>
    <span class="hljs-comment">// It can&apos;t be recovered.</span>
    SeverityFatal Severity = <span class="hljs-string">&quot;FATAL&quot;</span>
)

<span class="hljs-comment">// Verboser is an interface type that implements Info(f|ln) .</span>
<span class="hljs-comment">// See the documentation of V for more information.</span>
<span class="hljs-keyword">type</span> Verboser <span class="hljs-keyword">interface</span> {
    <span class="hljs-comment">// Info logs to the INFO log.</span>
    <span class="hljs-comment">// Arguments are handled in the manner of fmt.Println; a newline is appended if missing.</span>
    Info(...<span class="hljs-keyword">interface</span>{})
    <span class="hljs-comment">// Infof logs to the INFO log.</span>
    <span class="hljs-comment">// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.</span>
    Infof(<span class="hljs-keyword">string</span>, ...<span class="hljs-keyword">interface</span>{})
    <span class="hljs-comment">// Infoln logs to the INFO log.</span>
    <span class="hljs-comment">// Arguments are handled in the manner of fmt.Println; a newline is appended if missing.</span>
    Infoln(...<span class="hljs-keyword">interface</span>{})
}

<span class="hljs-comment">// Logger provides a set of methods to output log.</span>
<span class="hljs-keyword">type</span> Logger <span class="hljs-keyword">interface</span> {
    Verboser
    <span class="hljs-comment">// V reports whether verbosity at the call site is at least the requested level.</span>
    <span class="hljs-comment">// The returned value is a Verboser, which implements Info, Infof</span>
    <span class="hljs-comment">// and Infoln. These methods will write to the Info log if called.</span>
    V(Level) Verboser
    <span class="hljs-comment">// Warning logs to the WARNING logs.</span>
    <span class="hljs-comment">// Arguments are handled in the manner of fmt.Print; a newline is appended if missing.</span>
    Warning(...<span class="hljs-keyword">interface</span>{})
    <span class="hljs-comment">// Warningf logs to the WARNING logs.</span>
    <span class="hljs-comment">// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.</span>
    Warningf(<span class="hljs-keyword">string</span>, ...<span class="hljs-keyword">interface</span>{})
    <span class="hljs-comment">// Warningln logs to the WARNING logs.</span>
    <span class="hljs-comment">// Arguments are handled in the manner of fmt.Println; a newline is appended if missing.</span>
    Warningln(...<span class="hljs-keyword">interface</span>{})
    <span class="hljs-comment">// Error logs to the ERROR logs.</span>
    <span class="hljs-comment">// Arguments are handled in the manner of fmt.Print; a newline is appended if missing.</span>
    Error(...<span class="hljs-keyword">interface</span>{})
    <span class="hljs-comment">// Errorf logs to the ERROR logs.</span>
    <span class="hljs-comment">// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.</span>
    Errorf(<span class="hljs-keyword">string</span>, ...<span class="hljs-keyword">interface</span>{})
    <span class="hljs-comment">// Errorln logs to the ERROR logs.</span>
    <span class="hljs-comment">// Arguments are handled in the manner of fmt.Println; a newline is appended if missing.</span>
    Errorln(...<span class="hljs-keyword">interface</span>{})
    <span class="hljs-comment">// Fatal logs to the FATAL logs, then calls os.Exit(1).</span>
    <span class="hljs-comment">// Arguments are handled in the manner of fmt.Print; a newline is appended if missing.</span>
    Fatal(...<span class="hljs-keyword">interface</span>{})
    <span class="hljs-comment">// Fatalf logs to the FATAL logs, then calls os.Exit(1).</span>
    <span class="hljs-comment">// Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.</span>
    Fatalf(<span class="hljs-keyword">string</span>, ...<span class="hljs-keyword">interface</span>{})
    <span class="hljs-comment">// Fatalln logs to the FATAL logs, then calls os.Exit(1).</span>
    <span class="hljs-comment">// Arguments are handled in the manner of fmt.Println; a newline is appended if missing.</span>
    Fatalln(...<span class="hljs-keyword">interface</span>{})
    <span class="hljs-comment">// Clone clones current logger with new wrapper.</span>
    <span class="hljs-comment">// A positive wrapper indicates how many wrappers outside the logger.</span>
    <span class="hljs-comment">// A negative wrapper indicates how many wrappers should be stripped.</span>
    Clone(wrapper <span class="hljs-keyword">int</span>) Logger
}
</code></pre>
<p>&#x8FD9;&#x5957;&#x65E5;&#x5FD7; API &#x53C2;&#x8003;&#x4E86; glog &#x7684; golang API&#xFF0C;&#x5E76;&#x4E14;&#x5141;&#x8BB8;&#x7528;&#x6237;&#x63A5;&#x5165;&#x5176;&#x4ED6;&#x65E5;&#x5FD7;&#x7CFB;&#x7EDF;&#x3002;</p>
<p>&#x5728;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x81EA;&#x5B9A;&#x4E49;&#x7684; logger &#x4E4B;&#x524D;&#xFF0C;&#x9700;&#x8981;&#x77E5;&#x9053;&#x4EE5;&#x4E0B;&#x51E0;&#x4EF6;&#x4E8B;&#x60C5;&#xFF1A;</p>
<ol>
<li>log &#x5305;&#x6709;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x7684; logger&#xFF0C;&#x4F7F;&#x7528;&#x7684;&#x662F; StdLogger&#x3002;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <code>SetDefaultLogger()</code> &#x53D8;&#x66F4;&#x3002;</li>
<li>&#x5BF9;&#x4E8E;&#x4E0D;&#x9700;&#x8981;&#x8F93;&#x51FA;&#x65E5;&#x5FD7;&#x7684;&#x573A;&#x666F;&#xFF0C;&#x4F7F;&#x7528; SilentLogger &#x7684;&#x5B9E;&#x4F8B;&#x800C;&#x4E0D;&#x662F; nil.</li>
<li>&#x6B63;&#x786E;&#x5B9E;&#x73B0; logger &#x7684; Clone &#x65B9;&#x6CD5;&#x3002;Clone &#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x5728;&#x5BF9; logger &#x8FDB;&#x884C;&#x5305;&#x88C5;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x83B7;&#x53D6;&#x6B63;&#x786E;&#x5C42;&#x7EA7;&#x7684;&#x8C03;&#x7528;&#x6808;&#x4FE1;&#x606F;&#x3002;</li>
<li>&#x5728; Fatal* &#x65F6;&#x5019;&#xFF0C;&#x4F7F;&#x7528; os.Exit(1) &#x8FDB;&#x884C;&#x9000;&#x51FA;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x4F7F;&#x7528;&#x5176;&#x4ED6;&#x9519;&#x8BEF;&#x7801;&#xFF08;&#x9664;&#x975E;&#x4E1A;&#x52A1;&#x6709;&#x8981;&#x6C42;&#xFF09;&#x3002;&#x8FD9;&#x662F;&#x6807;&#x51C6;&#x5E93;&#x7684;&#x65E5;&#x5FD7;&#x5305;&#x4F7F;&#x7528;&#x7684;&#x9000;&#x51FA;&#x7801;&#x3002;</li>
</ol>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="start.html" class="navigation navigation-prev " aria-label="Previous page: 准备工作">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="errors.html" class="navigation navigation-next " aria-label="Next page: errors">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"log","level":"1.5.2","depth":2,"next":{"title":"errors","level":"1.5.3","depth":2,"path":"topics/errors.md","ref":"topics/errors.md","articles":[]},"previous":{"title":"准备工作","level":"1.5.1","depth":2,"path":"topics/start.md","ref":"topics/start.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"zh-hans","gitbook":"*"},"file":{"path":"topics/log.md","mtime":"2019-08-06T09:48:58.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-10-20T10:33:16.586Z"},"basePath":"..","book":{"language":"zh-hans"}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

